<?php include("dataprovider.php");
include ("Cart.php");
?>
<?php include("includes/function.php") ?>
<?php include("includes/header.php") ?>
<?php include("includes/banner.php") ?>

<div class="container">
    <div id="my-post">
    <?php
        switch($_REQUEST['cat']){
            case 1:
                $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where b.ProductCat = 'Búp bê'";
                $t = "Búp bê";
                break;
                case 2:
                $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where b.ProductCat = 'Xếp hình'";
                $t = "Xếp hình";
                break;
                case 3:
                $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where b.ProductCat = 'Xe đồ chơi'";
                $t = "Xe đồ chơi";
                break;
                case 4:
                $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where b.ProductCat = 'Đồ chơi thông minh'";
                $t = "Đồ chơi thông minh";
                break;
                case 5:
                $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where c.ManuName = 'LEGO'";
                $t = "Nhà sản xuất LEGO";
                break;
                case 6:
                $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where c.ManuName = 'Chicco'";
                $t = "Nhà sản xuất Chicco";
                break;
                case 7:
                $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where c.ManuName = 'Bright Starts'";
                $t = "Nhà sản xuất Bright Starts";
                break;
                case 8:
                $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where c.ManuName = 'MGA Entertainment'";
                $t = "Nhà sản xuất MGA Entertainment";
                break;
                case 9:
                $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where c.ManuName = 'Etic'";
                $t = "Nhà sản xuất Etic";
                break;
    }

            $r = mysqli_query($dbc, $q);
            confirm_query($r, $q);
            if ($r != false) {
                ?>
        <h1><?php echo $t; ?></h1>
        <div class="best-selling">
            
                <div id="product">
                    <ul>
                        <?php
                        $i = 0;
                        while ($row = mysqli_fetch_array($r, MYSQL_ASSOC)) {
                            $i += 1;
                            if($i > 10){
                                Break;
                            }
                            ?>
                            <li>
                                <a href="productdetail.php?detailid=<?php echo $row['ProductID'] ?>">
                                    <img src="<?php echo $row["ProductImg"]; ?>" width="150" height="150" alt="" />
                                    <p><?php echo $row["ProductName"]; ?></br>Giá: <red><?php echo number_format($row["ProductPrice"]) ?> VNÐ</red> </p></a>

                                <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post"
                                      name="form<?php echo $row['ProductID']; ?>" width="440px">

                                    <span>
                                        <input name="productid<?php echo $row['ProductID']; ?>" type="hidden" 
                                               value="<?php echo $row['ProductID']; ?>" />
                                        <input name="productname<?php echo $row['ProductID']; ?>" type="hidden"
                                               value="<?php echo $row['ProductName']; ?>" />
                                        <input name="productprice<?php echo $row['ProductID']; ?>" type="hidden"
                                               value="<?php echo $row['ProductPrice']; ?>" />
                                        <input name="quantity<?php echo $row['ProductID']; ?>" type="hidden"
                                               value="<?php echo '1'; ?>" />
                                        <input name="submit<?php echo $row['ProductID']; ?>" type="submit"  value = 
                                               "Thêm vào giỏ hàng" onclick="<?php
                                               if (isset($_SESSION['cartname']) == false) {
                                                   echo "alert('Bạn cần phải đăng nhập để mua hàng');";
                                               }
                                               if (isset($_REQUEST["productid" . $row['ProductID']]) && isset($_SESSION["cartname"]) == true) {
                                                   $_SESSION["cartname"]->InsertProduct($_REQUEST["productid" . $row['ProductID']], $_REQUEST["productname" . $row['ProductID']], $_REQUEST["quantity" . $row['ProductID']], $_REQUEST["productprice" . $row['ProductID']]);
                                                   echo "alert('Sản phẩm trong giỏ hàng: " . $_SESSION['cartname']->SumProduct() . " sản phẩm')";
                                               }
                                               ?>"/></span>
                                </form>
                            </li>
                        <?php }
                        ?>
                    </ul>
                </div>
                <?php
            } else {
                ?>
                <div id="gallery">
                    <p>Không có sản phẩm nào</p>
                </div>
                <?php
            }
            ?>
        </div>
    </div>
    
</div>
<?php include("includes/footer.php") ?>

